<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('订单列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="modal fade confirm-ship-modal" id="confirmShipModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title"><i class="fa fa-truck ship-warning-icon"></i>确认发货</h4>
                </div>
                <div class="modal-body">
                    <h4>是否收到订单款项准备发货？</h4>
                    <p class="text-muted">订单发货后无法撤销，请确认后完成发货</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirmShipBtn">确认发货</button>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>订单金额：</label>
                            <input type="text" name="orderAmount"/>
                        </li>
                        <li>
                            <label>买单客户：</label>
                            <input type="text" name="orderCustomer"/>
                        </li>
                        <li>
                            <label>删除标志：</label>
                            <input type="text" name="isDelete"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:orders:add">
                <i class="fa fa-plus"></i> 添加
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:orders:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:orders:remove')}]];
    var prefix = ctx + "system/orders";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            readUrl: prefix + "/read/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1; // 表格的索引从0开始，因此加1
                    }
                },
                {
                    field: 'orderAmount',
                    title: '订单金额'
                },
                {
                    field: 'orderCustomer',
                    title: '买单客户'
                },
                {
                    field: 'address',
                    title: '地址'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.read(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看</a> ');
                        actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="showShipConfirm(\'' + row.id + '\')"><i class="fa fa-paper-plane"></i>发货</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    // 显示发货确认弹窗
    var currentShipOrderId = null;
    function showShipConfirm(orderId) {
        currentShipOrderId = orderId;
        $('#confirmShipModal').modal('show');
    }

    // 确认发货操作
    $('#confirmShipBtn').click(function () {
        if (!currentShipOrderId) return;

        $.ajax({
            url: prefix + "/confirmShip/" + currentShipOrderId,
            type: 'POST',
            success: function (response) {
                if (response.code === 0) {
                    $.modal.msgSuccess("发货成功");
                    $('#confirmShipModal').modal('hide');
                    $.table.refresh(); // 刷新表格
                } else {
                    $.modal.alertError(response.msg || "发货失败");
                }
            },
            error: function () {
                $.modal.alertError("请求失败，请稍后再试");
            }
        });
    });
</script>
</body>
</html>